package com.example.windows8.newef.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.windows8.newef.R;
import com.example.windows8.newef.util.CriTextWatcher;
import com.example.windows8.newef.util.SharedUtil;
import com.example.windows8.newef.util.Httpget;

import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddCriActivity extends AppCompatActivity {
    @BindView(R.id.add_edit_content)
    EditText content;
    @BindView(R.id.add_tt_save)
    TextView save;
    @BindView(R.id.add_tt_send)
    TextView send;
    @BindView(R.id.add_tt_textnum)
    TextView textnum;
    @BindView(R.id.iv_back)
    ImageView back;
    private String uid;
    private String uname;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_cri);
        ButterKnife.bind(this);
        content.addTextChangedListener(new CriTextWatcher(content,textnum));
        content.setText(SharedUtil.getParam("cricontent","").toString());
        uid = SharedUtil.getParam("uid","").toString();
        uname = SharedUtil.getParam("uname","").toString();
    }
    @OnClick({R.id.iv_back,R.id.add_tt_send,R.id.add_tt_save})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.add_tt_save:
                SharedUtil.saveParam("cricontent", content.getText());
                finish();
                break;
            case R.id.add_tt_send:
                if(content.getText().length()!=0){
                    sendCri();
                }
                break;
        }
    }
    //发送朋友圈
    public void sendCri(){
        try {
            String sendcontent = content.getText().toString();
            String zson2  = "uid:\"" + uid
                    + "\",content:\"" + URLEncoder.encode(sendcontent, "UTF-8")
                    + "\",mtype:\"" + 1
                    + "\",uname:\"" + uname + "\"";
            new AsyncTask<String, Integer, JSONObject>() {
                @Override
                protected JSONObject doInBackground(String... strings) {
                    JSONObject result2 = new Httpget(strings[0],AddCriActivity.this,"1051").getResult();
                    return result2;
                }
                @Override
                protected void onPostExecute(JSONObject result) {
                    super.onPostExecute(result);
                    if(result == null){
                        Toast.makeText(AddCriActivity.this, "发表失败", Toast.LENGTH_SHORT).show();
                    }
                    else{
                        Toast.makeText(AddCriActivity.this, "发表成功", Toast.LENGTH_SHORT).show();
                        SharedUtil.saveParam("cricontent", "");
                        AddCriActivity.this.finish();
                    }
                }
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                }
            }.execute(zson2);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
